package com.dd.admin.controller.organization;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dd.common.convert.SysDeptConvert;
import com.dd.common.dto.system.dept.SysDeptDTO;
import com.dd.common.dto.system.dept.SysDeptQueryDTO;
import com.dd.common.entity.SysDept;
import com.dd.common.pojo.dto.PageDTO;
import com.dd.common.pojo.dto.group.CreateGroup;
import com.dd.common.pojo.dto.group.UpdateGroup;
import com.dd.common.service.SysDeptService;
import com.dd.common.utils.AjaxResult;
import com.dd.common.utils.PageResult;
import com.dd.common.validator.annotation.IDMust;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author cheng
 */
@SaIgnore
@Tag(name = "部门管理")
@Validated
@RequestMapping("/org/dept")
@RestController
public class SysDeptController {
    @Resource
    private SysDeptService sysDeptService;

    @Resource
    private SysDeptConvert sysDeptConvert;

    @Operation(summary = "部门新增", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "部门数据",
            required = true,
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysDeptDTO.class))
    ))
    @PostMapping("/add")
    public AjaxResult<?> add(@Validated(CreateGroup.class) @RequestBody SysDeptDTO dto) {
        return AjaxResult.success(sysDeptService.save(sysDeptConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "部门编辑", requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(
            description = "部门数据",
            content = @Content(mediaType = MediaType.APPLICATION_JSON_VALUE, schema = @Schema(implementation = SysDeptDTO.class))
    ))
    @PatchMapping("/edit")
    public AjaxResult<?> edit(@Validated(UpdateGroup.class) @RequestBody SysDeptDTO dto) {
        return AjaxResult.success(sysDeptService.updateById(sysDeptConvert.dtoToEntity(dto)));
    }

    @Operation(summary = "部门删除", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "部门ID", required = true))
    @DeleteMapping("/delete/{id}")
    public AjaxResult<?> delete(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysDeptService.removeById(id));
    }

    @Operation(summary = "部门详情", parameters = @Parameter(name = "id", in = ParameterIn.PATH, description = "部门ID", required = true))
    @GetMapping("/detail/{id}")
    public AjaxResult<?> detail(@PathVariable @IDMust Long id) {
        return AjaxResult.success(sysDeptService.getById(id));
    }

    @Operation(summary = "部门列表")
    @GetMapping("/list")
    public AjaxResult<?> list(@Validated SysDeptQueryDTO dto) {
        return AjaxResult.success(sysDeptService.list(new LambdaQueryWrapper<SysDept>()
                .eq(dto.getStatus() != null, SysDept::getStatus, dto.getStatus())
                .like(StringUtils.hasText(dto.getDeptName()), SysDept::getDeptName, dto.getDeptName())
                .orderByAsc(SysDept::getSort)
        ));
    }

    @Operation(summary = "部门分页列表")
    @GetMapping("/page")
    public AjaxResult<?> page(@Validated PageDTO pageDTO, @Validated SysDeptQueryDTO dto) {
        Page<SysDept> page = Page.of(pageDTO.getCurrent(), pageDTO.getSize());
        LambdaQueryWrapper<SysDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(dto.getStatus() != null, SysDept::getStatus, dto.getStatus())
                .like(StringUtils.hasText(dto.getDeptName()), SysDept::getDeptName, dto.getDeptName())
                .orderByAsc(SysDept::getSort);
        // mybatis-plus 默认的的分页结果是 total, current, size, records，pages
//        return AjaxResult.success(sysDeptService.page(page, queryWrapper));
        IPage<SysDept> result = sysDeptService.page(page, queryWrapper);
        return AjaxResult.success(PageResult.iPageHandle(result.getTotal(), result.getCurrent(), result.getSize(), result.getRecords()));
    }

}
